package com.kxf.recyclerviewtest;

import android.Manifest;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.tbruyelle.rxpermissions.Permission;
import com.tbruyelle.rxpermissions.RxPermissions;

import java.util.ArrayList;
import java.util.List;

import rx.functions.Action1;

public class MainActivity extends AppCompatActivity {

    private StateListenRecyclerView recyclerView = null;
    private TextView tv, tv_top2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //分别申请多个权限
        RxPermissions.getInstance(MainActivity.this)
                //分别申请多个权限时，使用requestEach
                .requestEach(Manifest.permission.INTERNET)
                .subscribe(new Action1<Permission>() {
                    @Override
                    public void call(Permission permission) {
                        Log.d("MainActivity", "permission=" + permission);
                    }
                });

        recyclerView = findViewById(R.id.recyclerView);
        tv = findViewById(R.id.tv);
        tv_top2 = findViewById(R.id.tv_top2);
        tv.setVisibility(View.GONE);
        recyclerView.addStateListeners(new StateListenRecyclerView.StateListener() {
            @Override
            public void onScrollToTop() {
                tv_top2.setVisibility(View.VISIBLE);
            }

            @Override
            public void onScrollToBottom() {

            }

            @Override
            public void onRefshPre() {
                tv.setText("释放开始刷新");
                tv.setVisibility(View.VISIBLE);
            }

            @Override
            public void onRefshFinish() {
                tv.setText("正在刷新");

                tv.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tv.setVisibility(View.GONE);
                    }
                }, 1000);
            }

            @Override
            public void onRefshCancel() {
                tv.setVisibility(View.GONE);
                Toast.makeText(MainActivity.this, "取消刷新", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onStartScrollToDown() {
            }

            @Override
            public void onStartScrollToUp() {
                tv_top2.setVisibility(View.GONE);
            }
        });
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add("item:" + i);
        }
        // 创建StaggeredGridLayoutManager实例
        StaggeredGridLayoutManager layoutManager =
                new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        List<String> listImg = new ArrayList<>();
        listImg.add("https://cdn2.jianshu.io/assets/default_avatar/3-9a2bcc21a5d89e21dafc73b39dc5f582.jpg");
        listImg.add("https://oimagea4.ydstatic.com/image?id=8601144713117005134&product=adpublish&w=520&h=347");
        listImg.add("https://img04.sogoucdn.com/app/a/100520093/cbabdbccbeaebffb-383bf39e10904088-ce27e12db8f39ead50bb3e7eb12ce20b.jpg");
        listImg.add("https://img04.sogoucdn.com/app/a/100520093/e154002750821088-37dc5468319bfb35-1fd93b65adbe1f8188c4d8bcbd9db96c.jpg");
        listImg.add("https://img04.sogoucdn.com/app/a/100520093/9e73b82522168ce1-2d958c5d952779e4-87921cdab4e27c7ec9b9403aec831002.jpg");
        listImg.add("https://img02.sogoucdn.com/app/a/100520093/e79a8706efd117f6-8855c006d7ef2114-91dd704f3196c248e5a3d93c06c1a078.jpg");
        listImg.add("https://img02.sogoucdn.com/app/a/100520093/00e343ac13d4f4ab-6aac3a051fff376b-59667faf4e48cbdae5d57e2c55a8a8a3.jpg");
        listImg.add("https://img04.sogoucdn.com/app/a/100520093/065961e44eee981b-4c547f0b862a208b-bfae3329700364603f069b71698f3c9c.jpg");
        listImg.add("https://img02.sogoucdn.com/app/a/100520093/d7036c9a17f5fda2-214d2a7e5a1ec191-4e9e5fdbb1943dd2c1699ba28d3dafd8.jpg");
        listImg.add("https://img04.sogoucdn.com/app/a/100520093/f215576d37293a3d-2eae2c0049ae22e2-3a76d1741510aa3b7089fd264643534f.jpg");
        listImg.add("https://img01.sogoucdn.com/app/a/100520093/c286ca7ecc6f7a79-b8111944479eaff1-2d88022567febd9961b0604d5ac4b908.jpg");
        listImg.add("https://img01.sogoucdn.com/app/a/100520093/fd9433adc4d8e5c1-354144c14274849b-31a97c46a385c796a825c53411a938ae.jpg");
        listImg.add("https://img04.sogoucdn.com/app/a/100520093/e154002750821088-37dc5468319bfb35-bee0780fa7865229bfdf9c3347bf9868.jpg");
        listImg.add("https://img02.sogoucdn.com/app/a/100520093/f9d5c084396d06f6-90e0186edb2b7e85-591616b01eaf36aa973bb138f2f41d94.jpg");

        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/0.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/1.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/2.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/3.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/4.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/5.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/6.jpg");
        listImg.add("https://gitee.com/kuangxuefeng/kxfres/raw/img20200822/img/hy/7.jpg");

        recyclerView.setAdapter(new GlideAdapter(this, list, listImg));
    }
}
